/*说明 type 自定义类型判断
type类型如下
{
  text 普通文本
  face 表情
  img 图片
  file 文件
  audio 语音
  position 地理位置
  custom 自定义消息
  tip 群提示消息
}*/   
//注意：返回的是数组对象
//把消息转换成Html
const convertMsgtoHtml = (msg) => {
  let html = [],
      elems, elem, type, content;
  elems = msg.getElems(); //获取消息包含的元素数组
  let count = elems.length;
  for (let i = 0; i < count; i++) {
      elem = elems[i];
      type = elem.getType();//获取元素类型
      content = elem.getContent();//获取元素对象
      switch (type) {
          case webim.MSG_ELEMENT_TYPE.TEXT:
              /*let eleHtml = convertTextMsgToHtml(content);
              //转义，防XSS
              html += webim.Tool.formatText2Html(eleHtml);*/
              html.push(convertTextMsgToHtml(content));
              break;
          case webim.MSG_ELEMENT_TYPE.FACE:
              html.push(convertFaceMsgToHtml(content));
              break;
          case webim.MSG_ELEMENT_TYPE.IMAGE:
              if (i <= count - 2) {
                  let customMsgElem = elems[i + 1];//获取保存图片名称的自定义消息elem
                  let imgName = customMsgElem.getContent().getData();//业务可以自定义保存字段，demo中采用data字段保存图片文件名
                  html.push(convertImageMsgToHtml(content, imgName));
                  i++;//下标向后移一位
              } else {
                  html.push(convertImageMsgToHtml(content));
              }
              break;
          case webim.MSG_ELEMENT_TYPE.SOUND:
              html.push(convertSoundMsgToHtml(content));
              break;
          case webim.MSG_ELEMENT_TYPE.FILE:
              html.push(convertFileMsgToHtml(content));
              break;
          case webim.MSG_ELEMENT_TYPE.LOCATION:
              html.push(convertLocationMsgToHtml(content));
              break;
          case webim.MSG_ELEMENT_TYPE.CUSTOM:
              /*let eleHtml = convertCustomMsgToHtml(content);
              //转义，防XSS
              html += webim.Tool.formatText2Html(eleHtml);*/
              html.push(convertCustomMsgToHtml(content));
              break;
          case webim.MSG_ELEMENT_TYPE.GROUP_TIP:
              /*let eleHtml = convertGroupTipMsgToHtml(content);
              //转义，防XSS
              html += webim.Tool.formatText2Html(eleHtml);*/
              html.push(convertGroupTipMsgToHtml(content));
              break;
          default:
              webim.Log.error('未知消息元素类型: elemType=' + type);
              break;
      }
  }
  return html;
}

//解析文本消息元素
function convertTextMsgToHtml(content) {
  //return content.getText();
  return {
    'type':'text',
    'text':content.getText()
  };
}
//解析表情消息元素
function convertFaceMsgToHtml(content) {
  let faceUrl = null;
  let data = content.getData();
  let index = webim.EmotionDataIndexs[data];

  let emotion = webim.Emotions[index];
  if (emotion && emotion[1]) {
      faceUrl = emotion[1];
  }
  if (faceUrl) {
      //return "<img src='" + faceUrl + "'/>";
      return {
        'type':'face',
        'faceUrl':faceUrl
      }
  } else {
      return data;
  }
}
//解析图片消息元素
function convertImageMsgToHtml(content, imageName) {
  let smallImage = content.getImage(webim.IMAGE_TYPE.SMALL);//小图
  let bigImage = content.getImage(webim.IMAGE_TYPE.LARGE);//大图
  let oriImage = content.getImage(webim.IMAGE_TYPE.ORIGIN);//原图
  if (!bigImage) {
      bigImage = smallImage;
  }
  if (!oriImage) {
      oriImage = smallImage;
  }
  //return "<img name='" + imageName + "' src='" + smallImage.getUrl() + "#" + bigImage.getUrl() + "#" + oriImage.getUrl() + "' style='CURSOR: hand' id='" + content.getImageId() + "' bigImgUrl='" + bigImage.getUrl() + "' onclick='imageClick(this)' />";
  return {
    'type':'img',
    'imageName':imageName,
    'smallImage':smallImage.getUrl(),
    'bigImage':bigImage.getUrl(),
    'oriImage':oriImage.getUrl(),
    'imageId':content.getImageId()
  }
}
//解析语音消息元素
function convertSoundMsgToHtml(content) {
  let second = content.getSecond();//获取语音时长
  let downUrl = content.getDownUrl();
  if (webim.BROWSER_INFO.type == 'ie' && parseInt(webim.BROWSER_INFO.ver) <= 8) {
      return '[这是一条语音消息]demo暂不支持ie8(含)以下浏览器播放语音,语音URL:' + downUrl;
  }
  //return '<audio id="uuid_'+content.uuid+'" src="' + downUrl + '" controls="controls" onplay="onChangePlayAudio(this)" preload="none"></audio>';
  return {
    'type':'audio',
    'audioId':content.uuid,
    'url':downUrl
  }
}
//解析文件消息元素
function convertFileMsgToHtml(content) {
  let fileSize, unitStr;
  fileSize = content.getSize();
  unitStr = "Byte";
  if (fileSize >= 1024) {
      fileSize = Math.round(fileSize / 1024);
      unitStr = "KB";
  }
  // return '<a href="' + content.getDownUrl() + '" title="点击下载文件" ><i class="glyphicon glyphicon-file">&nbsp;' + content.getName() + '(' + fileSize + unitStr + ')</i></a>';

  //return '<a href="javascript:;" onclick=\'webim.onDownFile("'+content.uuid+'")\' title="点击下载文件" ><i class="glyphicon glyphicon-file">&nbsp;' + content.name + '(' + fileSize + unitStr + ')</i></a>';
  return {
    'type':'file',
    'fileId':content.uuid,
    'downUrl':content.getDownUrl(),
    'name':content.name,
    'size':fileSize,
    'unitStr':unitStr
  }
}
//解析位置消息元素
function convertLocationMsgToHtml(content) {
  //return '经度=' + content.getLongitude() + ',纬度=' + content.getLatitude() + ',描述=' + content.getDesc();
  return {
      'type':'position',
      'longitude':content.getLongitude(),//经度
      'latitude':content.getLatitude(),//纬度
      'desc':content.getDesc()//描述
  }
}
//解析自定义消息元素
function convertCustomMsgToHtml(content) {
  let data = content.getData();//自定义数据
  let desc = content.getDesc();//描述信息
  let ext = content.getExt();//扩展信息
  //return "data=" + data + ", desc=" + desc + ", ext=" + ext;
  return {
    'type':'custom',
    'data':data,
    'desc':desc,
    'ext':ext
  }
}
//解析群提示消息元素
function convertGroupTipMsgToHtml(content) {
  let WEB_IM_GROUP_TIP_MAX_USER_COUNT = 10;
  let text = "";
  let maxIndex = WEB_IM_GROUP_TIP_MAX_USER_COUNT - 1;
  let opType, opUserId, userIdList;
  let groupMemberNum;
  opType = content.getOpType();//群提示消息类型（操作类型）
  opUserId = content.getOpUserId();//操作人id
  switch (opType) {
      case webim.GROUP_TIP_TYPE.JOIN://加入群
          userIdList = content.getUserIdList();
          //text += opUserId + "邀请了";
          for (let m in userIdList) {
              text += userIdList[m] + ",";
              if (userIdList.length > WEB_IM_GROUP_TIP_MAX_USER_COUNT && m == maxIndex) {
                  text += "等" + userIdList.length + "人";
                  break;
              }
          }
          text = text.substring(0, text.length - 1);
          text += "加入该群，当前群成员数：" + content.getGroupMemberNum();
          break;
      case webim.GROUP_TIP_TYPE.QUIT://退出群
          text += opUserId + "离开该群，当前群成员数：" + content.getGroupMemberNum();
          break;
      case webim.GROUP_TIP_TYPE.KICK://踢出群
          text += opUserId + "将";
          userIdList = content.getUserIdList();
          for (let m in userIdList) {
              text += userIdList[m] + ",";
              if (userIdList.length > WEB_IM_GROUP_TIP_MAX_USER_COUNT && m == maxIndex) {
                  text += "等" + userIdList.length + "人";
                  break;
              }
          }
          text += "踢出该群";
          break;
      case webim.GROUP_TIP_TYPE.SET_ADMIN://设置管理员
          text += opUserId + "将";
          userIdList = content.getUserIdList();
          for (let m in userIdList) {
              text += userIdList[m] + ",";
              if (userIdList.length > WEB_IM_GROUP_TIP_MAX_USER_COUNT && m == maxIndex) {
                  text += "等" + userIdList.length + "人";
                  break;
              }
          }
          text += "设为管理员";
          break;
      case webim.GROUP_TIP_TYPE.CANCEL_ADMIN://取消管理员
          text += opUserId + "取消";
          userIdList = content.getUserIdList();
          for (let m in userIdList) {
              text += userIdList[m] + ",";
              if (userIdList.length > WEB_IM_GROUP_TIP_MAX_USER_COUNT && m == maxIndex) {
                  text += "等" + userIdList.length + "人";
                  break;
              }
          }
          text += "的管理员资格";
          break;

      case webim.GROUP_TIP_TYPE.MODIFY_GROUP_INFO://群资料变更
          text += opUserId + "修改了群资料：";
          let groupInfoList = content.getGroupInfoList();
          let type, value;
          for (let m in groupInfoList) {
              type = groupInfoList[m].getType();
              value = groupInfoList[m].getValue();
              switch (type) {
                  case webim.GROUP_TIP_MODIFY_GROUP_INFO_TYPE.FACE_URL:
                      //text += "群头像为" + value + "; ";
                      text += "上传并修改了群头像;";
                      break;
                  case webim.GROUP_TIP_MODIFY_GROUP_INFO_TYPE.NAME:
                      text += "群名称为" + value + "; ";
                      break;
                  case webim.GROUP_TIP_MODIFY_GROUP_INFO_TYPE.OWNER:
                      text += "群主为" + value + "; ";
                      break;
                  case webim.GROUP_TIP_MODIFY_GROUP_INFO_TYPE.NOTIFICATION:
                      text += "群公告为" + value + "; ";
                      break;
                  case webim.GROUP_TIP_MODIFY_GROUP_INFO_TYPE.INTRODUCTION:
                      text += "群简介为" + value + "; ";
                      break;
                  default:
                      text += "未知信息为:type=" + type + ",value=" + value + "; ";
                      break;
              }
          }
          break;

      case webim.GROUP_TIP_TYPE.MODIFY_MEMBER_INFO://群成员资料变更(禁言时间)
          text += opUserId + "修改了群成员资料:";
          let memberInfoList = content.getMemberInfoList();
          let userId, shutupTime;
          for (let m in memberInfoList) {
              userId = memberInfoList[m].getUserId();
              shutupTime = memberInfoList[m].getShutupTime();
              text += userId + ": ";
              if (shutupTime != null && shutupTime !== undefined) {
                  if (shutupTime == 0) {
                      text += "取消禁言; ";
                  } else {
                      text += "禁言" + shutupTime + "秒; ";
                  }
              } else {
                  text += " shutupTime为空";
              }
              if (memberInfoList.length > WEB_IM_GROUP_TIP_MAX_USER_COUNT && m == maxIndex) {
                  text += "等" + memberInfoList.length + "人";
                  break;
              }
          }
          break;
      default:
          text += "未知群提示消息类型：type=" + opType;
          break;
  }
  return {
    'type':'tip',
    'text':text
  }
  //return text;
}
export default convertMsgtoHtml